package com.gui.listeners;

import com.util.ResourceLoader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 *
 * @author sithum
 */
public class RemoveRecordTableModelListener implements TableModelListener {

    private int captureColumn = Integer.parseInt(ResourceLoader.getInstance().getProperty("category.remove.column"));
    private List<Object> garbageData;
    private JTable table;

    public int getCaptureColumn() {
        return captureColumn;
    }

    public void setCaptureColumn(int captureColumn) {
        this.captureColumn = captureColumn;
    }

    public RemoveRecordTableModelListener(JTable table) {
        this.table = table;
        this.garbageData = new ArrayList<Object>();
        this.table.getModel().addTableModelListener(this);
    }

    /**
     * Perform an action when a TableModelEvent is fired.
     * @param javax.swing.event.TableModelEvent - e
     */
    public void tableChanged(TableModelEvent e) {
        int selectedRow = e.getFirstRow();
        int selectedColumn = e.getColumn();
        Object changedValue = table.getValueAt(selectedRow, selectedColumn);
        if (changedValue.toString().equalsIgnoreCase("true")) {
            garbageData.add(table.getValueAt(selectedRow, captureColumn));
        } else if (changedValue.toString().equalsIgnoreCase("false")) {
            garbageData.remove(garbageData.size() - 1);
        }
    }

    /**
     * @return the garbageData
     */
    public List<Object> getGarbageData() {
        return garbageData;
    }

    /**
     * @param garbageData the garbageData to set
     */
    public void setGarbageData(List<Object> garbageData) {
        this.garbageData = garbageData;
    }

    /**
     * @return the table
     */
    public JTable getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(JTable table) {
        this.table = table;
    }
}
